//
//  DYZLoginViewController.m
//  DYZCars
//
//  Created by MF on 16/8/18.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZLoginViewController.h"
#import "UILabel+Addition.h"
#import "UIButton+Addition.h"


@interface DYZLoginViewController ()

@property (nonatomic, weak)UITextField  *nameField;

@property (nonatomic, weak)UITextField *psdTextFidle;

@property (nonatomic, weak)UIButton *checkbox;
@end

@implementation DYZLoginViewController

- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
}

- (void)setupUI {
    [super viewDidLoad];
//   标题
    UILabel *titltLabel = [UILabel labelWithText:@"庆红车行" andTextColor: [UIColor blackColor] andFontSize:25];
    [self.view addSubview:titltLabel];
//    图标
    UIImageView *iconView = [[UIImageView alloc] init];
    UIImage *image = [UIImage imageNamed:@"dl_logo"];
    iconView.image = image;
    iconView.contentMode = UIViewContentModeCenter;
    [self.view addSubview:iconView];
//    帐号
    UILabel *nameLabel = [UILabel labelWithText:@"帐号" andTextColor:[UIColor blackColor] andFontSize:16];
    [nameLabel sizeToFit];
    [self.view addSubview:nameLabel];
//    账户名textfield
    UITextField *nameField = [[UITextField alloc] init];
    nameField.placeholder = @"手机号";
    [self.view addSubview:nameField];
    self.nameField = nameField;
//    用户密码
    UILabel *psdLabel = [UILabel labelWithText:@"密码" andTextColor:[UIColor blackColor] andFontSize:16];
    [psdLabel sizeToFit];
    [self.view addSubview:psdLabel];
//    密码textfield
    UITextField *psdTextFidle = [[UITextField alloc] init];
    psdTextFidle.placeholder = @"密码";
    psdTextFidle.secureTextEntry = YES;
    [self.view addSubview:psdTextFidle];
    self.psdTextFidle = psdTextFidle;
    
// 登录按钮
    UIButton *loginBtn = [UIButton buttonWithText:@"登录" andTextColor:[UIColor blueColor] backgroundColor:[UIColor yellowColor] andFontSize:20];
    [self.view addSubview:loginBtn];
//    记住密码勾选框
    UIButton *checkbox=[[UIButton alloc]initWithFrame:CGRectZero];
    [self.view addSubview:checkbox];
    [checkbox setBackgroundImage:[UIImage imageNamed:@"zc_unselect"] forState:UIControlStateNormal];
    self.checkbox = checkbox;
    //    记住按钮label
    UILabel *memoryLabel = [UILabel labelWithText:@"记住密码" andTextColor:[UIColor blackColor] andFontSize:17];
    [self.view addSubview:memoryLabel];
//    忘记密码按钮
    UIButton *memeryBtn = [UIButton buttonWithText:@"忘记密码" andTextColor:[UIColor blackColor] backgroundColor:nil andFontSize:17];
    [memeryBtn addTarget:self action:@selector(findOldPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:memeryBtn];
//    条款按钮
    UILabel *protocalLabel = [UILabel labelWithText:@"我已阅读并同意《使用条款和隐私政策》" andTextColor:[UIColor blackColor] andFontSize:14];
    [self.view addSubview:protocalLabel];
    
//    条款按钮勾选框
    UIButton *protocolCheckbox=[[UIButton alloc]initWithFrame:CGRectZero];
    [self.view addSubview:protocolCheckbox];
    [protocolCheckbox setBackgroundImage:[UIImage imageNamed:@"zc_unselect"] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(loginSucess:) forControlEvents:UIControlEventTouchUpInside];
    
    
//    自动布局
//    标题
    [titltLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.offset(50);
    }];
    
//    图标
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(titltLabel);
        make.top.equalTo(titltLabel.mas_bottom).offset(8);
        make.height.offset(150);
        make.width.offset(150);
    }];
    
//    帐号
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.offset(300);
    }];
    [nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_right).offset(8);
        make.width.offset(200);
        make.top.equalTo(nameLabel.mas_top);

    }];
    
//    密码
    
    [psdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(16);
    }];
    [psdTextFidle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(psdLabel.mas_right).offset(8);
        make.width.offset(200);
         make.top.equalTo(psdLabel.mas_top);
    }];
    
//    登录按钮
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(psdTextFidle.mas_bottom).offset(16);
        make.left.offset(20);
        make.right.offset(-20);
        make.height.offset(40);
    }];
//    勾选框
    [checkbox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(loginBtn);
        make.top.equalTo(loginBtn.mas_bottom).offset(30);
        make.height.offset(20);
        make.width.offset(20);
    }];
//    记住密码
    [memoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(checkbox.mas_right).offset(8);
        make.top.equalTo(loginBtn.mas_bottom).offset(30);
    }];
    
//    忘记密码按钮
    [memeryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(loginBtn);
        make.top.equalTo(memoryLabel);
    }];
//    条款按钮
    [protocalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.offset(-16);
    }];
    [protocolCheckbox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(protocalLabel.mas_left).offset(-8);
        make.top.equalTo(protocalLabel);
        make.height.offset(20);
        make.width.offset(20);
    }];
}
//密码判断
- (void)loginSucess:(UIButton *)sender {
    if ([self.nameField.text isEqualToString:@""] && [self.psdTextFidle.text isEqualToString:@""]) {
        
            [UIApplication sharedApplication].keyWindow.rootViewController = [[NSClassFromString(@"DYZTabbarController") alloc] init];
    }else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:true completion:nil];
       
        
    }
}
//忘记密码
- (void)findOldPassword {
    NSLog(@"忘记密码");

    [self presentViewController:[self forgetPasswordViewController] animated:true completion:nil];

    

}
//忘记密码的控制器
-(UIViewController *)forgetPasswordViewController {
    UIViewController *vc = [[NSClassFromString(@"DYZForgetPsdController") alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    return nav;
}




@end
